<?php

/**
 * controler para almacenes
 *
 * @author jreyes
 */



require_once 'class/im/dlv/DP/SoDeliveryDP.class.php';
require_once 'class/im/dlv/DP/SoDeliveryLineaDP.class.php';
require_once 'class/im/dlv/FN/SoDeliveryFN.class.php';
require_once 'class/common/mc/FN/XxMaestroListaFN.class.php';
require_once 'class/im/alm/FN/ImAlmacenFN.class.php'; 

require_once($_SERVER['DOCUMENT_ROOT'].'/gourmet-express/trunk/src/class/im/mov/DP/ImStockMovimDP.class.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/gourmet-express/trunk/src/class/im/mov/DP/ImStockDP.class.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/gourmet-express/trunk/src/class/im/mov/FN/ImStockFN.class.php');

class SoDlvController extends ControllerBase
{
	
	public function listarSoDelivery(){
		
	   $o_SoDeliveryFN= new SoDeliveryFN();       
		
	   $page = isset($this->requestVars['page']) ? $this->requestVars['page'] : 1;
	   $rp = isset($this->requestVars['rp']) ? $this->requestVars['rp'] : 10;
	   $sortname = isset($this->requestVars['sortname']) ? $this->requestVars['sortname'] : 'name';
	   $sortorder = isset($this->requestVars['sortorder']) ? $this->requestVars['sortorder'] : 'desc';		
	   $o_SoDeliveryFN->page = $page;
	   $o_SoDeliveryFN->tamPag = $rp;
	   $o_SoDeliveryFN->sortCampos = $sortname;
	   $o_SoDeliveryFN->sortType   = $sortorder;
        if ($this->requestVars){
            if (array_key_exists('soDeliveryId', $this->requestVars)) {
                $o_SoDeliveryFN->setId($this->requestVars["soDeliveryId"]);
            }
            if (array_key_exists('clienteNombre', $this->requestVars)) {
                $o_SoDeliveryFN->setclienteNombre($this->requestVars["clienteNombre"]);  
            }      
            if (array_key_exists('estado', $this->requestVars)) {
                $o_SoDeliveryFN->setStatus($this->requestVars["estado"]);  
            }                     
        }
        $result = $o_SoDeliveryFN->select();     
        $vars['result'] = $result;
        $vars['soDeliveryFN'] = $o_SoDeliveryFN;
        if (array_key_exists('modoBusqueda', $this->requestVars)) {
            $vars['modoBusqueda'] = $this->requestVars["modoBusqueda"];
        }else{
            $vars['modoBusqueda'] = "";
        }
        $this->view->show("im/dlv/SoDeliveryLista",$vars);
		
	}
	
    public function listarSoDeliveryForm(){
		if (!isset($vars)){
			$vars = '';
		}        
        $this->view->show("im/dlv/SoDeliveryListaForm",$vars);
    }
    
    public function shw(){
    	$this->shwForm("view");
    }
    
    public function nuevo(){
           
        $o_XxXxMaestroListaFN = new XxMaestroListaFN();
        
        $o_XxXxMaestroListaFN->setListaId('tipoSalida');
        $ao_TipoIngreso = $o_XxXxMaestroListaFN->select();
        
        $o_XxXxMaestroListaFN->setListaId('monedaPO');
        $ao_Moneda      = $o_XxXxMaestroListaFN->select();
        
        $o_ImAlmacenFN = new ImAlmacenFN();
        $o_ImAlmacenFN->setParaCompraSw('Y');
        $ao_ImAlmacenBN = $o_ImAlmacenFN->select();
                       
        $a_Maestros['Moneda']              = $ao_Moneda;
        $a_Maestros['listaTipoSalida']    = $ao_TipoIngreso;        
        $a_Maestros['listaAlmacenes']      = $ao_ImAlmacenBN;        
        $vars['a_Maestros']                = $a_Maestros;
        $this->view->show("im/dlv/ordenDespachoNuevo",$vars);       
    }
    
    
    public function editar(){
       if ($this->requestVars)
       {   
          if (array_key_exists('soId', $this->requestVars)) 
          {     
            $o_SoDeliveryFN = new SoDeliveryFN();
            $o_SoDeliveryFN->setId($this->requestVars["soId"]);
            $o_SoDeliveryBN = $o_SoDeliveryFN->getSoDelivery();
        
            $o_XxXxMaestroListaFN = new XxMaestroListaFN();
            
            $o_XxXxMaestroListaFN->setListaId('tipoSalida');
            $ao_TipoIngreso = $o_XxXxMaestroListaFN->select();
            
            $o_XxXxMaestroListaFN->setListaId('monedaPO');
            $ao_Moneda      = $o_XxXxMaestroListaFN->select();
            
            $o_ImAlmacenFN = new ImAlmacenFN();
            $o_ImAlmacenFN->setParaCompraSw('Y');
            $ao_ImAlmacenBN = $o_ImAlmacenFN->select();
                           
            $a_Maestros['o_SoDeliveryBN']     = $o_SoDeliveryBN;
            $a_Maestros['Moneda']              = $ao_Moneda;
            $a_Maestros['listaTipoSalida']    = $ao_TipoIngreso;        
            $a_Maestros['listaAlmacenes']      = $ao_ImAlmacenBN;        
            $vars['a_Maestros']                = $a_Maestros;
            $this->view->show("im/dlv/ordenDespacho",$vars); 
          }
       }      
    }
    
    public function editarSave()
    {
      try 
      {
        if ($this->requestVars){

              $o_SoDeliveryDP                = new SoDeliveryDP();
              $o_SoDeliveryDP->setId($this->requestVars["soId"]);
              $o_SoDeliveryDP->eliminarSoDeliveryCabeceraDetalle(); // eliminar Recepcion
              
              
              $o_SoDeliveryDP->setTipo($this->requestVars["tipo"]);
              $o_SoDeliveryDP->setDlvNum($this->requestVars["poNro"]);
              $o_SoDeliveryDP->setAlmId($this->requestVars["almId"]);
              $o_SoDeliveryDP->setClienteId($this->requestVars["clienteId"]);
              $o_SoDeliveryDP->setClienteNombre($this->requestVars["clienteNombre"]);
              $o_SoDeliveryDP->setContacto($this->requestVars["contacto"]);
              $o_SoDeliveryDP->setDocReferNum($this->requestVars["docRef"]);
              $o_SoDeliveryDP->setFacturaNum($this->requestVars["facturaNro"]);
              $o_SoDeliveryDP->setMoneda($this->requestVars["moneda"]);
              $o_SoDeliveryDP->setFechaRequerida($this->requestVars["fechaReq"]);
              $o_SoDeliveryDP->setFechaProgramada($this->requestVars["fecProgram"]);
              $o_SoDeliveryDP->setFechaSalida($this->requestVars["fechaSalida"]);
              $o_SoDeliveryDP->setFlete($this->requestVars["flete"]);
              $o_SoDeliveryDP->setObs($this->requestVars["obs"]);
              
              $o_SoDeliveryDP->update();
              
              if($this->requestVars["a_ItemId"] )
              {
                  $i=1;
                  foreach ($this->requestVars["a_ItemId"] as $iKey => $value)
                  {
                    $o_SoDeliveryLineaDP = new SoDeliveryLineaDP();
                 
                    $o_SoDeliveryLineaDP->setDlvId($o_SoDeliveryDP->getId());	      
                    $o_SoDeliveryLineaDP->setLineaId($i);               
                    $o_SoDeliveryLineaDP->setArtId($this->requestVars["a_ItemId"][$iKey]);                        
                    $o_SoDeliveryLineaDP->setCant($this->requestVars["a_Cant"][$iKey]);
                    $o_SoDeliveryLineaDP->setCosto($this->requestVars["a_Costo"][$iKey]);
                    $o_SoDeliveryLineaDP->setCreaFecha(date('Y-m-d'));
                    $o_SoDeliveryLineaDP->save();
                    
                    $i++;
                  }
              }
              $a_result['dlvId'] = $this->requestVars["soId"];
              $a_result['rpta'] = 'true';
              $a_result['msg'] = '';
              
          }
      }catch (Exception $e)
      {
        $a_result['rpta'] = 'false';
        $a_result['msg'] = $e->getMessage().' en '.$e->getFile().' linea '.$e->getLine();        
      }
      
      echo json_encode($a_result);
    
    }

    
    public function guardarSave()
    {
      try 
      {
        if ($this->requestVars){
              $o_SoDeliveryDP                = new SoDeliveryDP();
                        
              $o_SoDeliveryDP->setTipo($this->requestVars["tipo"]);
              $o_SoDeliveryDP->setDlvNum($this->requestVars["poNro"]);
              $o_SoDeliveryDP->setAlmId($this->requestVars["almId"]);
              $o_SoDeliveryDP->setClienteId($this->requestVars["clienteId"]);
              $o_SoDeliveryDP->setClienteNombre($this->requestVars["clienteNombre"]);
              $o_SoDeliveryDP->setContacto($this->requestVars["contacto"]);
              $o_SoDeliveryDP->setDocReferNum($this->requestVars["docRef"]);
              $o_SoDeliveryDP->setFacturaNum($this->requestVars["facturaNro"]);
              $o_SoDeliveryDP->setMoneda($this->requestVars["moneda"]);
              $o_SoDeliveryDP->setFechaRequerida($this->requestVars["fechaReq"]);
              $o_SoDeliveryDP->setFechaProgramada($this->requestVars["fecProgram"]);
              $o_SoDeliveryDP->setFechaSalida($this->requestVars["fechaSalida"]);
              $o_SoDeliveryDP->setFlete($this->requestVars["flete"]);
              $o_SoDeliveryDP->setObs($this->requestVars["obs"]);
              
              $o_SoDeliveryDP->save();
              
              if($this->requestVars["a_ItemId"] )
              {
                  $i=1;
                  foreach ($this->requestVars["a_ItemId"] as $iKey => $value)
                  {
                    $o_SoDeliveryLineaDP = new SoDeliveryLineaDP();
                 
                    $o_SoDeliveryLineaDP->setDlvId($o_SoDeliveryDP->getId());	      
                    $o_SoDeliveryLineaDP->setLineaId($i);               
                    $o_SoDeliveryLineaDP->setArtId($this->requestVars["a_ItemId"][$iKey]);                        
                    $o_SoDeliveryLineaDP->setCant($this->requestVars["a_Cant"][$iKey]);
                    $o_SoDeliveryLineaDP->setCosto($this->requestVars["a_Costo"][$iKey]);
                    $o_SoDeliveryLineaDP->setCreaFecha(date('Y-m-d'));
                    $o_SoDeliveryLineaDP->save();
                    
                    $i++;
                  }
              }
              $a_result['dlvId'] = $o_SoDeliveryDP->getId();
              $a_result['rpta'] = 'true';
              $a_result['msg'] = '';
              
          }
      }catch (Exception $e)
      {
        $a_result['rpta'] = 'false';
        $a_result['msg'] = $e->getMessage().' en '.$e->getFile().' linea '.$e->getLine();        
      }
      
      echo json_encode($a_result);
    
    }
    
    public function cerrar(){
       if ($this->requestVars)
       {   
          if (array_key_exists('soId', $this->requestVars)) 
          {     
            $o_SoDeliveryFN = new SoDeliveryFN();
            $o_SoDeliveryFN->setId($this->requestVars["soId"]);
            $o_SoDeliveryBN = $o_SoDeliveryFN->getSoDelivery();                            
          }
       }
       if ($o_SoDeliveryBN->getStatus() != 'C'){
           $a_detalleRcp = $o_SoDeliveryBN->getObj_SoDeliveryLinea();
           
           foreach ($a_detalleRcp as $iKey => $value)
           { 
               $o_ImStockFN= new ImStockFN();
               $o_ImStockDP  = new ImStockDP();
               $o_ImStockDP->setAlmId($o_SoDeliveryBN->getAlmId()) ;   
               $o_ImStockDP->setArtId($value->getArtId());
               
               $o_ImStockFN->setAlmId($o_SoDeliveryBN->getAlmId());
               $o_ImStockFN->setArtId($value->getArtId());
               $o_ImStockBN = $o_ImStockFN->selectByPK();
               if ($o_ImStockBN){
                   if ($o_ImStockBN->getCantFisico() < $value->getCant()){
                       $a_result['rpta'] = false;
                       $a_result['msg'] = "Stock insuficiente para el articulo ".$value->getArtId();
                   }           
               }
           }                                 
           if (!$a_result['msg']){
                foreach ($a_detalleRcp as $iKey => $value)
                { 
                    $o_ImStockMovimDP  = new ImStockMovimDP();
                    $o_ImStockMovimDP->setAlmId($o_SoDeliveryBN->getAlmId());
                    $o_ImStockMovimDP->setArtId($value->getArtId());
                    $o_ImStockMovimDP->setCant($value->getCant()*(-1));
                    $o_ImStockMovimDP->setCosto($value->getCosto());
                    $o_ImStockMovimDP->setCreaFecha(date("Y-m-d H:i:s"));
                    $o_ImStockMovimDP->setCreaUsr($value->getCreaUsr());
                    $o_ImStockMovimDP->setDocId($o_SoDeliveryBN->getDlvNum());
                    $o_ImStockMovimDP->setDocNum($o_SoDeliveryBN->getDlvNum());
                    $o_ImStockMovimDP->setDocReferenNro($o_SoDeliveryBN->getFacturaNum());
                    $o_ImStockMovimDP->setIngSal("S");
                    $o_ImStockMovimDP->setMovFecha(date("Y-m-d"));
                    $o_ImStockMovimDP->setMovTipo("D");
                    $o_ImStockMovimDP->save();
                    $o_ImStockFN= new ImStockFN();

                    $o_ImStockDP  = new ImStockDP();
                    $o_ImStockDP->setAlmId($o_SoDeliveryBN->getAlmId()) ;   
                    $o_ImStockDP->setArtId($value->getArtId());

                    $o_ImStockFN->setAlmId($o_SoDeliveryBN->getAlmId());
                    $o_ImStockFN->setArtId($value->getArtId());
                    $o_ImStockBN = $o_ImStockFN->selectByPK();
                    if ($o_ImStockBN){
                        if ($o_ImStockBN->getCantFisico() >= $value->getCant()){
                            $o_ImStockDP->setCantFisico($o_ImStockBN->getCantFisico() - $value->getCant());
                            $o_ImStockDP->update();
                        }              
                    }
                    $result = $o_ImStockFN->select();
                }
                $o_SoDeliveryDP = new SoDeliveryDP();
                $o_SoDeliveryDP->setId($this->requestVars["soId"]);       
                $o_SoDeliveryDP->setStatus("C");
                $o_SoDeliveryDP->update();
                $vars['result'] = $result;
                $vars['stockFN'] = $o_ImStockFN;
                $a_result['soId'] = $this->requestVars["soId"];
                $a_result['rpta'] = 'true';
           }
       }else{
           $a_result['rpta'] = false;           
       }
       echo json_encode($a_result);
    }    
    
}
?>